package com.ideaction.game.limiter;

import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.ideaction.game.globalconst.KeyPrefix;

import lombok.extern.slf4j.Slf4j;

/**
 * 限流器
 * @author ZGame
 *
 *
 */
@Slf4j
@Service
public class RedissionUserLimiter {
	@Autowired
	private RedissonClient redissonClient;
	private int seconds = 10;
	private int passNum = 1;

	@Cacheable(cacheNames = "limiter",key = "#uid")
	public RRateLimiter getUserLimiter(String uid) {
		RRateLimiter limiter = redissonClient.getRateLimiter(KeyPrefix.UID_LIMITER);
		limiter.trySetRate(RateType.OVERALL, passNum, seconds, RateIntervalUnit.SECONDS);
		return limiter;
	}
}
